package com.medical.acc.service.impl;

import com.medical.acc.service.LevelService;
import com.medical.acc.mapper.LevelMapper;
import com.medical.acc.model.Level;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/10.
 */
@Service("levelService")
public class LevelServiceImpl implements LevelService {
    @Autowired
    private LevelMapper levelMapper;

    public List<Level> selectAllLevel() {
        return levelMapper.selectAll();
    }
    public void deleteByPrimaryKey(int id){
        if(levelMapper.selectByPrimaryKey(id)==null){
            throw new PeopleException("不存在此套餐等级");
        }
        levelMapper.deleteByPrimaryKey(id);
    }

    public void saveLevel(Level level) {

        if(level!=null){
            if(StringUtils.isEmpty(level.getName())) {
                throw new PeopleException("名字不能为空");
            }
            if(StringUtils.isEmpty(level.getDescription())){
                throw new PeopleException("备注不能为空");
            }
            List<Level> levels=levelMapper.selectAll();
            for(Level l:levels){
                if (l.getName().equals(level.getName())){
                    throw new PeopleException("已存在此套餐");
                }
            }
            levelMapper.saveLevel(level);
            }else {
                throw new PeopleException("等级信息为空");
            }

    }

    public Level selectByPrimaryKey(int id) {
        return levelMapper.selectByPrimaryKey(id);
    }

    public Level selectByName(String name) {
        return levelMapper.selectByName(name);
    }

    public void updateByPrimaryKey(Level level) {

        if(level!=null){
            if(StringUtils.isEmpty(level.getName())) {
                throw new PeopleException("名字不能为空");
            }
            if(StringUtils.isEmpty(level.getDescription())){
                throw new PeopleException("备注不能为空");
            }
            List<Level> levels=levelMapper.selectAll();
            for(Level l:levels){
                if (l.getName().equals(level.getName())) {
                    if (l.getName().equals(levelMapper.selectByPrimaryKey(level.getId()).getName())) {

                    }else {
                        throw new PeopleException("已存在此套餐");
                    }
                }
            }
            levelMapper.updateByPrimaryKey(level);
        }else {
            throw new PeopleException("等级信息为空");
        }

    }
}
